<template>
  <div class="pt-10">
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <h1 class="text-2xl text-center font-bold mt-6 mb-6">登录</h1>
        <van-field
          v-model="loginForm.username"
          name="username"
          label="用户名"
          left-icon="user"
          placeholder="请输入用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="loginForm.password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          left-icon="lock"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div class="text-right leading-[3.6rem] mr-3">
          没有账号？<a class="text-blue-700 underline" href="/register"
            >前往注册</a
          >
        </div>
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { useUserStore } from "@/store/modules/user";
import { reactive } from "vue";

const userStore = useUserStore();
const router = useRouter();

const loginForm = reactive({
  phone: "",
  username: "",
  password: "",
});

const onSubmit = async () => {
  await userStore.login(loginForm);
  router.replace("/");
};
</script>
